package com.ray.ray.util;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

public class APNManager {

	// ���Ų�������url�����?�˿�
	public static String mmscUrl_ct = "http://mmsc.vnet.mobi";
	public static String mmsProxy_ct = "10.0.0.200";
	// �ƶ���������url�����?�˿�
	public static String mmscUrl_cm = "http://mmsc.monternet.com";
	public static String mmsProxy_cm = "10.0.0.172";
	// jͨ��������url�����?�˿�
	public static String mmscUrl_uni = "http://mmsc.vnet.mobi";
	public static String mmsProxy_uni = "10.0.0.172";

	private static String TAG = "APNManager";
	private static final Uri APN_TABLE_URI = Uri
			.parse("content://telephony/carriers");// ���е�APN��������Ϣλ��
	private static final Uri PREFERRED_APN_URI = Uri
			.parse("content://telephony/carriers/preferapn");// ��ǰ��APN
	private static String[] projection = { "_id", "apn", "type", "current",
			"proxy", "port" };
	private static String APN_NET_ID = null;
	private static String APN_WAP_ID = null;

	public static List<String> getSimMNC(Context context) {
		TelephonyManager telManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String imsi = telManager.getSubscriberId();
		if (imsi != null) {
			ArrayList<String> list = new ArrayList<String>();
			if (imsi.startsWith("46000") || imsi.startsWith("46002")) {
				// ��Ϊ�ƶ�������46000�µ�IMSI�Ѿ����꣬����������һ��46002��ţ�134/159�Ŷ�ʹ���˴˱��
				// �й��ƶ�
				list.add(mmscUrl_cm);
				list.add(mmsProxy_cm);
			} else if (imsi.startsWith("46001")) {
				// �й�jͨ
				list.add(mmscUrl_uni);
				list.add(mmsProxy_uni);
			} else if (imsi.startsWith("46003")) {
				// �й����
				list.add(mmscUrl_ct);
				list.add(mmsProxy_ct);
			}
			shouldChangeApn(context);
			return list;
		}
		return null;
	}

	private static boolean shouldChangeApn(final Context context) {

		final String wapId = getWapApnId(context);
		String apnId = getCurApnId(context);
		// ��ǰapn����wap�����л���wap
		if (!wapId.equals(apnId)) {
			APN_NET_ID = apnId;
			setApn(context, wapId);
			// �л�apn��Ҫһ��ʱ�䣬���õȴ�2��
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return true;
		}
		return false;
	}

	public static boolean shouldChangeApnBack(final Context context) {
		// ���ŷ�����Ϻ����Ƿ���Ҫ�ѽ�����л���4
		if (null != APN_NET_ID) {
			setApn(context, APN_NET_ID);
			return true;
		}
		return false;
	}

	// �л���NETAPN
	public static boolean ChangeNetApn(final Context context) {
		final String wapId = getWapApnId(context);
		String apnId = getCurApnId(context);
		// ��ǰapn��wap�����л���net
		if (wapId.equals(apnId)) {
			APN_NET_ID = getNetApnId(context);
			setApn(context, APN_NET_ID);
			// �л�apn��Ҫһ��ʱ�䣬���õȴ��룬����������й�
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return true;
		}
		return true;
	}

	// �л���WAPAPN
	public static boolean ChangeWapApn(final Context context) {
		final String netId = getWapApnId(context);
		String apnId = getCurApnId(context);
		// ��ǰapn��net�����л���wap
		if (netId.equals(apnId)) {
			APN_WAP_ID = getNetApnId(context);
			setApn(context, APN_WAP_ID);
			// �л�apn��Ҫһ��ʱ�䣬���õȴ��룬����������й�
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return true;
		}
		return true;
	}

	// ��ȡ��ǰAPN
	public static String getCurApnId(Context context) {
		ContentResolver resoler = context.getContentResolver();
		// String[] projection = new String[] { "_id" };
		Cursor cur = resoler.query(PREFERRED_APN_URI, projection, null, null,
				null);
		String apnId = null;
		if (cur != null && cur.moveToFirst()) {
			apnId = cur.getString(cur.getColumnIndex("_id"));
		}
		Log.i("xml", "getCurApnId:" + apnId);
		return apnId;
	}

	public static APN getCurApnInfo(final Context context) {
		ContentResolver resoler = context.getContentResolver();
		// String[] projection = new String[] { "_id" };
		Cursor cur = resoler.query(PREFERRED_APN_URI, projection, null, null,
				null);
		APN apn = new APN();
		if (cur != null && cur.moveToFirst()) {
			apn.id = cur.getString(cur.getColumnIndex("_id"));
			apn.apn = cur.getString(cur.getColumnIndex("apn"));
			apn.type = cur.getString(cur.getColumnIndex("type"));

		}
		return apn;
	}

	public static void setApn(Context context, String id) {
		ContentResolver resolver = context.getContentResolver();
		ContentValues values = new ContentValues();
		values.put("apn_id", id);
		resolver.update(PREFERRED_APN_URI, values, null, null);
	}

	// ��ȡWAP APN
	public static String getWapApnId(Context context) {
		ContentResolver contentResolver = context.getContentResolver();
		// ��ѯcmwapAPN
		Cursor cur = contentResolver.query(APN_TABLE_URI, projection,
				"apn = \'ctwap\' and current = 1", null, null);
		// wap APN �˿ڲ�Ϊ��
		if (cur != null && cur.moveToFirst()) {
			do {
				String id = cur.getString(cur.getColumnIndex("_id"));
				String proxy = cur.getString(cur.getColumnIndex("proxy"));
				if (null != proxy) {
					Log.i("xml", "getWapApnId" + id);
					return id;
				}
			} while (cur.moveToNext());
		}
		return null;
	}

	public static String getNetApnId(Context context) {
		ContentResolver contentResolver = context.getContentResolver();
		Cursor cur = contentResolver.query(APN_TABLE_URI, projection,
				"apn = \'cmnet\' and current = 1", null, null);
		if (cur != null && cur.moveToFirst()) {
			return cur.getString(cur.getColumnIndex("_id"));
		}
		return null;
	}

	// ��ȡ����APN
	public static ArrayList<APN> getAPNList(final Context context) {

		ContentResolver contentResolver = context.getContentResolver();
		Cursor cr = contentResolver.query(APN_TABLE_URI, projection, null,
				null, null);

		ArrayList<APN> apnList = new ArrayList<APN>();

		if (cr != null && cr.moveToFirst()) {
			do {
				APN apn = new APN();
				apn.id = cr.getString(cr.getColumnIndex("_id"));
				apn.apn = cr.getString(cr.getColumnIndex("apn"));
				apn.type = cr.getString(cr.getColumnIndex("type"));
				apnList.add(apn);
			} while (cr.moveToNext());

			cr.close();
		}
		return apnList;
	}

	// ��ȡ���õ�APN
	public static ArrayList<APN> getAvailableAPNList(final Context context) {
		// current��Ϊ�ձ�ʾ����ʹ�õ�APN
		ContentResolver contentResolver = context.getContentResolver();
		Cursor cr = contentResolver.query(APN_TABLE_URI, projection,
				"current is not null", null, null);
		ArrayList<APN> apnList = new ArrayList<APN>();
		if (cr != null && cr.moveToFirst()) {
			do {
				APN apn = new APN();
				apn.id = cr.getString(cr.getColumnIndex("_id"));
				apn.apn = cr.getString(cr.getColumnIndex("apn"));
				apn.type = cr.getString(cr.getColumnIndex("type"));
				apnList.add(apn);
			} while (cr.moveToNext());

			cr.close();
		}
		return apnList;

	}

	// �Զ���APN��װ��
	static class APN {

		String id;

		String apn;

		String type;

		@Override
		public String toString() {
			return "id=" + id + ",apn=" + apn + ";type=" + type;
		}
	}

}
